package dev.zeno.booru;

import java.util.List;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Base64;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;

public class BooruBrowserActivity extends BaseActivity {
	
	ListView lv;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        lv = (ListView) findViewById(R.id.listview);
        lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Booru booru = (Booru) arg0.getItemAtPosition(arg2);
				booru.setType(Booru.GELBOORU);
				startActivity(new Intent(BooruBrowserActivity.this, ViewBooruActivity.class).putExtra("ID", booru.getId()));
			}
		});
        lv.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				
				Booru booru = (Booru) arg0.getItemAtPosition(arg2);
				AlertDialog.Builder alert = new AlertDialog.Builder(BooruBrowserActivity.this);
				alert.setItems(new String[] {"Edit", "Delete"}, new OnClickListener() {
					int id;
					@Override
					public void onClick(DialogInterface dialog, int which) {
						switch(which)
						{
						case 0:
							//TODO Add edit
							break;
						case 1:
							booruDatabase.open();
							booruDatabase.delete(id);
							booruDatabase.close();
							populateList();
							break;
						}
						dialog.dismiss();
					}
					
					public OnClickListener setId(int id)
					{
						this.id = id;
						return this;
					}
				}.setId(booru.getId()));
				alert.show();
				return true;
			}
		});
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getSupportMenuInflater();
        inflater.inflate(R.menu.mainmenu, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId())
        {
	        case R.id.add_booru:
	        	addNewBooru();
	        	return true;
        	default:
        		return super.onOptionsItemSelected(item);
        }
    }
    
    @Override
    public void onResume()
    {
    	populateList();
        super.onResume();
    }

	private void populateList() {
		booruDatabase.open();
        List<Booru> items = booruDatabase.getBoorus();
        booruDatabase.close();
        lv.setAdapter(new BooruListAdapter(items));
	}
    
    class BooruListAdapter extends BaseAdapter
    {
    	
    	List<Booru> items;

    	public BooruListAdapter(List<Booru> items)
    	{
    		this.items = items;
    	}
    	
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return items.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			if(arg0 < items.size())
				return items.get(arg0);
			else
				return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			View v;
			LayoutInflater inflater = getLayoutInflater();
			
			Booru item = items.get(position);
			
			v = inflater.inflate(R.layout.booru_cell, parent, false);
			TextView name = (TextView) v.findViewById(R.id.text1);
			TextView url = (TextView) v.findViewById(R.id.text2);
			name.setText(item.getName());
			url.setText(item.getUrl());
			
			if(item.getFavicon() != null && item.getFavicon().length() > 0)
			{
				byte[] bytes = Base64.decode(item.getFavicon(), Base64.DEFAULT);
				Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
				ImageView img = (ImageView) v.findViewById(R.id.favicon_view);
				img.setImageBitmap(bmp);
			}
			
			

			return v;
		}
    	
    }
    
    public void addNewBooru()
    {
    	startActivityForResult(new Intent(this, AddNewActivity.class), NEW_BOORU_REQUEST);
    }
}